package com.heima.freemarker.controller;

import com.heima.freemarker.pojo.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/basic")
public class FreeController {
    /**
     * 测试普通文本和对象转换为静态资源
     * @param model
     * @return
     */
    @GetMapping
    public String testModel(Model model){
        model.addAttribute("name","欢迎来到英雄联盟!");
        //创建一个学生对象
        Student stu = new Student();
        stu.setName("张三");
        stu.setAge(23);
        stu.setMoney(888D);
        model.addAttribute("stu",stu);
        return "01-basic";
    }

    @GetMapping("/list")
    public String testListAndMap(Model model){
        //创建两个学生对象
        Student stu1 = new Student();
        stu1.setName("张三");
        stu1.setAge(23);
        stu1.setMoney(888D);

        Student stu2 = new Student();
        stu2.setName("李四");
        stu2.setAge(24);
        stu2.setMoney(999D);

        //创建list集合,将对象存入集合中
        List<Student> list = new ArrayList<>();
        list.add(stu1);
        list.add(stu2);
        //将list添加到model中
        model.addAttribute("stus",list);
        //创建map集合将对象存入集合中
        Map<String,Student> map = new HashMap<>();
        map.put("stu1",stu1);
        map.put("stu2",stu2);
        model.addAttribute("stuMap",map);
        return "02-list";
    }
}
